﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;

namespace CIRDiagnoseDevice
{
    class PdfReportM1HeaderFooter:PdfPageEventHelper
    {
        public string header = "";
        public int presentFontSize = 12;  //页脚字体大小
        public Rectangle pageSize = PageSize.A4; //默认文档大小为A4
        public PdfTemplate total;
        public BaseFont bf = null;
        public Font fontDetail = null;

        public PdfReportM1HeaderFooter(string yejiao, int presentFontSize, Rectangle pageSize)
        {
            this.header = yejiao;
            this.presentFontSize = presentFontSize;
            this.pageSize = pageSize;
        }

        public PdfReportM1HeaderFooter()
        {

        }

        public void SetHeader(string header)
        {
            this.header = header;
        }

        public void setPresentFontSize(int presentFontSize)
        {
            this.presentFontSize = presentFontSize;
        }

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            total = writer.DirectContent.CreateTemplate(50, 50); // 共 页 的矩形的长宽高
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            try
            {
                if (bf == null)
                {
                    bf = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                }
                if (fontDetail == null)
                {
                    fontDetail = new Font(bf, presentFontSize, Font.NORMAL); // 数据字体
                }

            }
            catch
            {

            }
            // 1.写入页眉
            ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.Left, document.Top + 20, 0);
            // 2.写入前半部分的 第X页/共
            int pages = writer.PageNumber;
            string foot1 = "第" + pages + " 页/共";
            Phrase footer = new Phrase(foot1, fontDetail);
            // 3.计算前半部分的foot1的长度， 后面好定位最后一部分的'Y页'这俩字的x轴坐标，字体长度也要计算进去 = len
            float len = bf.GetWidthPoint(foot1, presentFontSize);
            //  4.得到当前的PdfContentByte
            PdfContentByte cb = writer.DirectContent;

            // 5.写入页脚1，x轴就是(右margin+左margin + right() -left()- len)/2.0F 再给偏移20F适合人类视觉感受，否则肉眼看上去就太偏左了 ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了；注意Y轴是从下往上累加的，最上方的Top值是大于Bottom好几百开外的。
            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer, (document.RightMargin + document.Right + document.LeftMargin - document.Left - len) / 2.0f + 20f, document.Bottom - 20, 0);
            // 6.写入页脚2的模板（就是页脚的Y页这俩字）添加到文档中，计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len ， y 轴和之前的保持一致，底边界-20
            cb.AddTemplate(total, (document.RightMargin + document.Right + document.LeftMargin - document.Left) / 2.0f + 20f, document.Bottom - 20);  // 调整模板显示位置

        }

        public override void  OnCloseDocument(PdfWriter writer, Document document)
        {
            total.BeginText();
            total.SetFontAndSize(bf, presentFontSize);
            string foot2 = " " + (writer.PageNumber - 1)+ " 页";
            total.ShowText(foot2);
            total.EndText();
            total.ClosePath();
        }
    }
}
